﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Wheeler.Common.Extensions;
using Wheeler.Web.ViewModels;
using Wheeler.Domain;
using System.Text;

namespace Wheeler.Web.Utility
{
    public static class ApplicationCache
    {
        //private const String CATEGORIES = "Categories";
        private const String USER_GROUPS = "UserGroups";        

        /// <summary>
        /// Cache of User Groups
        /// </summary>
        public static List<GroupViewModel> UserGroups
        {
            get
            {
                List<GroupViewModel> userGroups = HttpContext.Current.Application[USER_GROUPS] as List<GroupViewModel>;
                if (userGroups == null || userGroups.Count == 0)
                {
                    RefreshUserGroups();
                    userGroups = HttpContext.Current.Application[USER_GROUPS] as List<GroupViewModel>;
                }
                return userGroups;
            }
        }

        /// <summary>
        /// Refreshes the user group cache
        /// </summary>
        public static void RefreshUserGroups()
        {
            WheelerEntities context = new WheelerEntities();
            List<Group> groups = context.Groups.ToList();
            List<GroupViewModel> groupsViewModel = AutoMapper.Mapper.Map<List<Group>, List<GroupViewModel>>(groups);
            HttpContext.Current.Application[USER_GROUPS] = groupsViewModel;
        }

        /// <summary>
        /// Mime Types for different file extension. Currently holds only the content types for resume documents.
        /// </summary>
        public static Dictionary<String, String> MimeTypes
        {
            get
            {
                Dictionary<String, String> configs = HttpContext.Current.Application["JB_MIME_TYPES"] as Dictionary<String, String>;
                if (configs == null || configs.Count == 0)
                {
                    InitMimeTypes();
                    configs = HttpContext.Current.Application["JB_MIME_TYPES"] as Dictionary<String, String>;
                }
                return configs;
            }
        }

        /// <summary>
        /// Initializes Supported MIME Types by JobBoard
        /// </summary>
        public static void InitMimeTypes()
        {
            Dictionary<String, String> mimeTypes = new Dictionary<String, String>
            {
                //'Images'
                //{".bmp", "image/bmp"},
                //{".gif", "image/gif"},
                //{".jpeg", "image/jpeg"},
                //{".jpg", "image/jpeg"},
                //{".png", "image/png"},
                //{".tif", "image/tiff"},
                //{".tiff", "image/tiff"},
                //'Documents'
                {".doc", "application/msword"},
                {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
                {".pdf", "application/pdf"},
                {".txt", "text/plain"},
                //'Slideshows'
                //{".ppt", "application/vnd.ms-powerpoint"},
                //{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
                //'Data'
                //{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
                //{".xls", "application/vnd.ms-excel"},
                //{".csv", "text/csv"},
                //{".xml", "text/xml"},                
                //'Compressed Folders'
                //{".zip", "application/zip"},
                //'Audio'
                //{".ogg", "application/ogg"},
                //{".mp3", "audio/mpeg"},
                //{".wma", "audio/x-ms-wma"},
                //{".wav", "audio/x-wav"},
                //'Video'
                //{".wmv", "audio/x-ms-wmv"},
                //{".swf", "application/x-shockwave-flash"},
                //{".avi", "video/avi"},
                //{".mp4", "video/mp4"},
                //{".mpeg", "video/mpeg"},
                //{".mpg", "video/mpeg"},
                //{".qt", "video/quicktime"}
            };
            HttpContext.Current.Application["JB_MIME_TYPES"] = mimeTypes;
        }
    }
}